module Sound.Manager;
private import System.Console;
/**
*
*/


/**
*	サウンド＝音響空間に描画するオブジェクトであると考える
*/
public interface ISoundSprite {
	float x();
	float y();
	float z();
	void x( float xx );
	void y( float yy );
	void z( float zz );
	bool visible();
	void visible( bool f );
	int volume();
	void  volume( int v );

	bool busy();
	void load( string src ) ;
	string src();
	int  repeat();
	void repeat( int v ) ;
	
	ISoundSprite play();
	ISoundSprite play( string src);
	ISoundSprite play( string src , int repeat );
	ISoundSprite stop();
	ISoundSprite pause();
	ISoundSprite resume();
	ISoundSprite reset();
	ISoundSprite release();
	ISoundSprite call();
	
	ISoundSprite moveTo( float xx , float yy);
	
} //


public class SoundManager {
	private static ISoundSprite _bgm;
	private static ISoundSprite[] _pool;
	private static int _soundVolume = 100;
	private static int _sptr ;
	private static ISoundSprite delegate() _factory;
	public static void setSystem ( ISoundSprite bgm , ISoundSprite delegate() factory , int maxcnt ) {
		_bgm = bgm;
		_sptr = 0;
		_factory = factory;
		for ( int i = 0 ; i < maxcnt ; i++ ) {
			_pool ~= factory();
		}
	} //

	public static int bgmVolume() {
		return _bgm.volume;
	} //

	public static void bgmVolume( int v ) {
		_bgm.volume = v;
	} //

	public static int soundVolume() {
		return _soundVolume;
	} //

	public static void soundVolume( int v ) {
		foreach ( int i , ISoundSprite s ; _pool ) {
			s.volume = v;
		}
		_soundVolume = v;
	} //

	public static ISoundSprite background() { 
		return _bgm;
	} //

	public static void clear() { 
		int len = _pool.length;
		for( int i = 0 ; i < len ; i ++ ) {
			_pool[ i ].stop();
		}
	} //

	public static ISoundSprite getSprite() {
		ISoundSprite ret = DummySoundSprite.instance;
		int len = _pool.length;
		for( int i = 0 ; i < len ; i ++ ) {
			_sptr = ( _sptr + 1 ) % len;
				ret = _pool[ _sptr ];
				ret.call();
				break;
		}
		return ret;
	}

} //




public class DummySoundSprite : ISoundSprite {
	public static DummySoundSprite instance;
	private static this(){
		instance = new DummySoundSprite();
	} //
	public float x(){return 0;}
	public float y(){return 0;}
	public float z(){return 0;}
	public void x( float xx ){}
	public void y( float yy ){}
	public void z( float zz ){}
	public bool visible(){return true;}
	public void visible( bool f ){}
	public int volume(){return 100;}
	public void  volume( int v ){}

	public bool busy(){return true;}
	public void load( string src ){}
	public string src(){return "";}
	public int  repeat(){return 0;}
	public void repeat( int v ){}
	
	public ISoundSprite play(){return this;}
	public ISoundSprite play( string src ){return this;}
	public ISoundSprite play( string src , int repeat ){return this;}
	public ISoundSprite pause(){return this;}
	public ISoundSprite resume(){return this;}
	public ISoundSprite stop(){return this;}
	public ISoundSprite reset(){return this;}
	public ISoundSprite release(){return this;}
	public ISoundSprite call(){return this;}
	
	ISoundSprite moveTo( float xx , float yy){return this;}
		
} //

